//-----------------------------------------------------------------------
// <copyright file="a_eCard_MsgDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_eCard_MsgDAOBase
    {
        #region Common methods
        public virtual a_eCard_Msg Createa_eCard_MsgFromReader(IDataReader reader)
        {
            a_eCard_Msg item = new a_eCard_Msg();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("MsgID"))) item.MsgID = (int)reader["MsgID"];
                if (!reader.IsDBNull(reader.GetOrdinal("AccessCode"))) item.AccessCode = (int)reader["AccessCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserID"))) item.UserID = (int)reader["UserID"];
                if (!reader.IsDBNull(reader.GetOrdinal("FromName"))) item.FromName = (string)reader["FromName"];
                if (!reader.IsDBNull(reader.GetOrdinal("FromEmail"))) item.FromEmail = (string)reader["FromEmail"];
                if (!reader.IsDBNull(reader.GetOrdinal("ToName"))) item.ToName = (string)reader["ToName"];
                if (!reader.IsDBNull(reader.GetOrdinal("ToEmail"))) item.ToEmail = (string)reader["ToEmail"];
                if (!reader.IsDBNull(reader.GetOrdinal("MsgHeading"))) item.MsgHeading = (string)reader["MsgHeading"];
                if (!reader.IsDBNull(reader.GetOrdinal("MsgText"))) item.MsgText = (string)reader["MsgText"];
                if (!reader.IsDBNull(reader.GetOrdinal("ImageName"))) item.ImageName = (string)reader["ImageName"];
                if (!reader.IsDBNull(reader.GetOrdinal("SoundName"))) item.SoundName = (string)reader["SoundName"];
                if (!reader.IsDBNull(reader.GetOrdinal("Layout"))) item.Layout = (byte)reader["Layout"];
                if (!reader.IsDBNull(reader.GetOrdinal("Style"))) item.Style = (byte)reader["Style"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsNotifySender"))) item.IsNotifySender = (bool)reader["IsNotifySender"];
                if (!reader.IsDBNull(reader.GetOrdinal("ArticleBackLink"))) item.ArticleBackLink = (string)reader["ArticleBackLink"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateCreated"))) item.DateCreated = (DateTime)reader["DateCreated"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateExpire"))) item.DateExpire = (DateTime)reader["DateExpire"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_eCard_Msg methods
            
        public virtual void Createa_eCard_Msg(a_eCard_Msg a_eCard_Msg)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_eCard_Msgs_ins");
                
                database.AddInParameter(dbCommand, "@AccessCode", DbType.Int32, a_eCard_Msg.AccessCode);
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_eCard_Msg.UserID);
                database.AddInParameter(dbCommand, "@FromName", DbType.String, a_eCard_Msg.FromName);
                database.AddInParameter(dbCommand, "@FromEmail", DbType.AnsiString, a_eCard_Msg.FromEmail);
                database.AddInParameter(dbCommand, "@ToName", DbType.String, a_eCard_Msg.ToName);
                database.AddInParameter(dbCommand, "@ToEmail", DbType.AnsiString, a_eCard_Msg.ToEmail);
                database.AddInParameter(dbCommand, "@MsgHeading", DbType.String, a_eCard_Msg.MsgHeading);
                database.AddInParameter(dbCommand, "@MsgText", DbType.String, a_eCard_Msg.MsgText);
                database.AddInParameter(dbCommand, "@ImageName", DbType.AnsiString, a_eCard_Msg.ImageName);
                database.AddInParameter(dbCommand, "@SoundName", DbType.AnsiString, a_eCard_Msg.SoundName);
                database.AddInParameter(dbCommand, "@Layout", DbType.Byte, a_eCard_Msg.Layout);
                database.AddInParameter(dbCommand, "@Style", DbType.Byte, a_eCard_Msg.Style);
                database.AddInParameter(dbCommand, "@IsNotifySender", DbType.Boolean, a_eCard_Msg.IsNotifySender);
                database.AddInParameter(dbCommand, "@ArticleBackLink", DbType.AnsiString, a_eCard_Msg.ArticleBackLink);
                database.AddInParameter(dbCommand, "@DateCreated", DbType.DateTime, a_eCard_Msg.DateCreated);
                database.AddInParameter(dbCommand, "@DateExpire", DbType.DateTime, a_eCard_Msg.DateExpire);
                database.AddOutParameter(dbCommand, "@MsgID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_eCard_Msg.MsgID = (int)database.GetParameterValue(dbCommand, "@MsgID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_eCard_Msg methods
        
        public virtual void Updatea_eCard_Msg(a_eCard_Msg a_eCard_Msg)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_eCard_Msgs_upd");            
                
                database.AddInParameter(dbCommand, "@MsgID", DbType.Int32, a_eCard_Msg.MsgID);
                database.AddInParameter(dbCommand, "@AccessCode", DbType.Int32, a_eCard_Msg.AccessCode);
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_eCard_Msg.UserID);
                database.AddInParameter(dbCommand, "@FromName", DbType.String, a_eCard_Msg.FromName);
                database.AddInParameter(dbCommand, "@FromEmail", DbType.AnsiString, a_eCard_Msg.FromEmail);
                database.AddInParameter(dbCommand, "@ToName", DbType.String, a_eCard_Msg.ToName);
                database.AddInParameter(dbCommand, "@ToEmail", DbType.AnsiString, a_eCard_Msg.ToEmail);
                database.AddInParameter(dbCommand, "@MsgHeading", DbType.String, a_eCard_Msg.MsgHeading);
                database.AddInParameter(dbCommand, "@MsgText", DbType.String, a_eCard_Msg.MsgText);
                database.AddInParameter(dbCommand, "@ImageName", DbType.AnsiString, a_eCard_Msg.ImageName);
                database.AddInParameter(dbCommand, "@SoundName", DbType.AnsiString, a_eCard_Msg.SoundName);
                database.AddInParameter(dbCommand, "@Layout", DbType.Byte, a_eCard_Msg.Layout);
                database.AddInParameter(dbCommand, "@Style", DbType.Byte, a_eCard_Msg.Style);
                database.AddInParameter(dbCommand, "@IsNotifySender", DbType.Boolean, a_eCard_Msg.IsNotifySender);
                database.AddInParameter(dbCommand, "@ArticleBackLink", DbType.AnsiString, a_eCard_Msg.ArticleBackLink);
                database.AddInParameter(dbCommand, "@DateCreated", DbType.DateTime, a_eCard_Msg.DateCreated);
                database.AddInParameter(dbCommand, "@DateExpire", DbType.DateTime, a_eCard_Msg.DateExpire);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_eCard_Msg methods
        public virtual void Deletea_eCard_Msg(int msgID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_eCard_Msgs_del");
                
                database.AddInParameter(dbCommand, "@MsgID", DbType.Int32, msgID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_eCard_Msg methods
        
        public virtual a_eCard_Msg Geta_eCard_Msg(int msgID, int accessCode)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_eCard_Msgs_sel");
                
                database.AddInParameter(dbCommand, "@MsgID", DbType.Int32, msgID);
                database.AddInParameter(dbCommand, "@AccessCode", DbType.Int32, accessCode);
                
                a_eCard_Msg a_eCard_Msg =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_eCard_Msg = Createa_eCard_MsgFromReader(reader);
                        reader.Close();
                    }
                }
                return a_eCard_Msg;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_eCard_MsgList methods
        public virtual a_eCard_MsgCollection Geta_eCard_MsgList(a_eCard_MsgColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_eCard_Msgs_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_eCard_MsgCollection a_eCard_MsgCollection = new a_eCard_MsgCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_eCard_Msg a_eCard_Msg = Createa_eCard_MsgFromReader(reader);
                        a_eCard_MsgCollection.Add(a_eCard_Msg);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_eCard_MsgCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_eCard_MsgCollection Geta_eCard_MsgList(a_eCard_MsgColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_eCard_MsgList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion

        #region Ngoc
        public virtual void UpdateOrInserta_eCard_Msg(string action, string userEmail, int docId, a_eCard_Msg a_eCard_Msg, out int msgId, out int accessCode)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_eCard_Upd");
                
                database.AddInParameter(dbCommand, "@Action", DbType.AnsiString, action);
                database.AddInParameter(dbCommand, "@UserEmail", DbType.AnsiString, userEmail);
                database.AddInParameter(dbCommand, "@FromName", DbType.String, a_eCard_Msg.FromName);
                database.AddInParameter(dbCommand, "@FromEmail", DbType.AnsiString, a_eCard_Msg.FromEmail);
                database.AddInParameter(dbCommand, "@ToName", DbType.String, a_eCard_Msg.ToName);
                database.AddInParameter(dbCommand, "@ToEmail", DbType.AnsiString, a_eCard_Msg.ToEmail);
                database.AddInParameter(dbCommand, "@MsgHeading", DbType.String, a_eCard_Msg.MsgHeading);
                database.AddInParameter(dbCommand, "@MsgText", DbType.String, a_eCard_Msg.MsgText);
                database.AddInParameter(dbCommand, "@ImageName", DbType.AnsiString, a_eCard_Msg.ImageName);
                database.AddInParameter(dbCommand, "@SoundName", DbType.AnsiString, a_eCard_Msg.SoundName);
                database.AddInParameter(dbCommand, "@Layout", DbType.Int32, a_eCard_Msg.Layout);
                database.AddInParameter(dbCommand, "@Style", DbType.Int32, a_eCard_Msg.Style);
                database.AddInParameter(dbCommand, "@IsNotifySender", DbType.Boolean, a_eCard_Msg.IsNotifySender);
                database.AddInParameter(dbCommand, "@ArticleBackLink", DbType.AnsiString, a_eCard_Msg.ArticleBackLink);
                //database.AddInParameter(dbCommand, "@DateCreated", DbType.DateTime, a_eCard_Msg.DateCreated);
                database.AddInParameter(dbCommand, "@DateExpire", DbType.DateTime, a_eCard_Msg.DateExpire);
                database.AddInParameter(dbCommand, "@DocID", DbType.Int32, docId);
                database.AddOutParameter(dbCommand, "@MsgID", DbType.Int32, 4);
                database.AddOutParameter(dbCommand, "@AccessCode", DbType.Int32, 4);

                database.ExecuteNonQuery(dbCommand);
                msgId = (int)database.GetParameterValue(dbCommand, "@MsgID");
                accessCode = (int)database.GetParameterValue(dbCommand, "@AccessCode");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}